package org.htmlparser.lexer;

import java.io.IOException;
import java.io.InputStream;

public class Stream extends InputStream
  implements Runnable
{
  protected static final int BUFFER_SIZE = 4096;
  protected static final int EOF = -1;
  public int fills;
  public volatile byte[] mBuffer;
  protected int mContentLength;
  protected volatile InputStream mIn;
  public volatile int mLevel;
  protected int mMark;
  protected int mOffset;
  public int reallocations;
  public int synchronous;

  public Stream(InputStream paramInputStream)
  {
    this(paramInputStream, 0);
  }

  public Stream(InputStream paramInputStream, int paramInt)
  {
    this.fills = 0;
    this.reallocations = 0;
    this.synchronous = 0;
    this.mIn = paramInputStream;
    this.mBuffer = null;
    this.mLevel = 0;
    this.mOffset = 0;
    if (paramInt < 0)
      i = 0;
    while (true)
    {
      this.mContentLength = i;
      this.mMark = -1;
      return;
      i = paramInt;
    }
  }

  public int available()
    throws IOException
  {
    return (this.mLevel - this.mOffset);
  }

  /**
   * @deprecated
   */
  public void close()
    throws IOException
  {
    monitorenter;
    try
    {
      if (this.mIn != null)
      {
        this.mIn.close();
        this.mIn = null;
      }
      this.mBuffer = null;
      this.mLevel = 0;
      this.mOffset = 0;
      this.mContentLength = 0;
      this.mMark = -1;
      monitorexit;
      return;
    }
    finally
    {
      localObject = finally;
      monitorexit;
      throw localObject;
    }
  }

  /**
   * @deprecated
   */
  protected boolean fill(boolean paramBoolean)
    throws IOException
  {
    monitorenter;
    int i = 0;
    while (true)
    {
      try
      {
        if (this.mIn != null)
        {
          int k;
          byte[] arrayOfByte;
          int l;
          if (!(paramBoolean))
          {
            int i1 = available();
            if (i1 != 0)
            {
              j = 1;
              monitorexit;
              return j;
            }
            this.synchronous = (1 + this.synchronous);
          }
          if (this.mContentLength == 0)
            if (this.mBuffer == null)
            {
              this.mBuffer = new byte[Math.max(4096, this.mIn.available())];
              arrayOfByte = this.mBuffer;
              k = arrayOfByte.length - this.mLevel;
            }
          while (true)
          {
            while (true)
            {
              while (true)
              {
                l = this.mIn.read(arrayOfByte, this.mLevel, k);
                if (-1 != l)
                  break label222;
                this.mIn.close();
                this.mIn = null;
                break label323:
                if (this.mBuffer.length - this.mLevel >= 2048)
                  break;
                arrayOfByte = new byte[Math.max(2 * this.mBuffer.length, this.mBuffer.length + this.mIn.available())];
              }
              arrayOfByte = this.mBuffer;
            }
            k = this.mContentLength - this.mLevel;
            if (this.mBuffer == null)
              this.mBuffer = new byte[k];
            arrayOfByte = this.mBuffer;
          }
          if (this.mBuffer != arrayOfByte)
          {
            label222: System.arraycopy(this.mBuffer, 0, arrayOfByte, 0, this.mLevel);
            this.mBuffer = arrayOfByte;
            this.reallocations = (1 + this.reallocations);
          }
          this.mLevel = (l + this.mLevel);
          if ((this.mContentLength != 0) && (this.mLevel == this.mContentLength))
          {
            this.mIn.close();
            this.mIn = null;
          }
          i = 1;
        }
      }
      finally
      {
        monitorexit;
      }
      label323: int j = i;
    }
  }

  public void mark(int paramInt)
  {
    this.mMark = this.mOffset;
  }

  public boolean markSupported()
  {
    return true;
  }

  public int read()
    throws IOException
  {
    int i;
    if (this.mLevel - this.mOffset == 0)
      fill(false);
    if (this.mLevel - this.mOffset != 0)
    {
      byte[] arrayOfByte = this.mBuffer;
      int j = this.mOffset;
      this.mOffset = (j + 1);
      i = 0xFF & arrayOfByte[j];
    }
    while (true)
    {
      return i;
      i = -1;
    }
  }

  public void reset()
    throws IOException
  {
    if (-1 != this.mMark)
      this.mOffset = this.mMark;
    while (true)
    {
      return;
      this.mOffset = 0;
    }
  }

  public void run()
  {
    boolean bool1;
    boolean bool2;
    try
    {
      do
      {
        bool2 = fill(true);
        bool1 = bool2;
      }
      while (bool1);
      return;
    }
    catch (IOException localIOException)
    {
      localIOException.printStackTrace();
      bool1 = false;
    }
  }
}